<template>
  <div class="admin-layout">
    <el-container>
      <!-- 侧边栏 -->
      <el-aside width="240px" class="sidebar">
        <div class="logo">
          <h3>视频管理系统</h3>
        </div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical-dark"
          router
        >
          <el-menu-item index="/dashboard">
            <el-icon><Document /></el-icon>
            <span>仪表盘</span>
          </el-menu-item>
          <el-menu-item index="/categories">
            <el-icon><Menu /></el-icon>
            <span>视频类别管理</span>
          </el-menu-item>
          <el-menu-item index="/video-types">
            <el-icon><VideoCamera /></el-icon>
            <span>视频类型管理</span>
          </el-menu-item>
          <el-menu-item index="/videos">
            <el-icon><VideoPlay /></el-icon>
            <span>视频管理</span>
          </el-menu-item>
          <el-menu-item index="/comments">
            <el-icon><ChatRound /></el-icon>
            <span>评论管理</span>
          </el-menu-item>
          <el-menu-item index="/users">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/settings">
            <el-icon><Setting /></el-icon>
            <span>系统设置</span>
          </el-menu-item>
        </el-menu>
      </el-aside>

      <!-- 主内容区 -->
      <el-container>
        <!-- 顶部导航 -->
        <el-header class="header">
          <div class="header-left">
            <span>管理员控制台</span>
          </div>
          <div class="header-right">
            <span class="user-info">{{ userInfo.username }}</span>
            <el-button type="text" @click="handleLogout">
              <el-icon><SwitchButton /></el-icon>
              退出登录
            </el-button>
          </div>
        </el-header>

        <!-- 内容区 -->
        <el-main class="main-content">
          <div class="dashboard-content">
            <h2>欢迎回来，{{ userInfo.nickname || userInfo.username }}</h2>
            <div class="stats-cards">
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">128</div>
                  <div class="stat-label">用户总数</div>
                </div>
              </el-card>
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">356</div>
                  <div class="stat-label">视频总数</div>
                </div>
              </el-card>
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">8</div>
                  <div class="stat-label">类别总数</div>
                </div>
              </el-card>
              <el-card class="stat-card">
                <div class="stat-content">
                  <div class="stat-number">2.5K</div>
                  <div class="stat-label">评论总数</div>
                </div>
              </el-card>
            </div>
            
            <div class="recent-activities">
              <h3>最近活动</h3>
              <el-table :data="recentActivities" style="width: 100%">
                <el-table-column prop="time" label="时间" width="180"></el-table-column>
                <el-table-column prop="description" label="描述"></el-table-column>
                <el-table-column prop="user" label="操作人" width="120"></el-table-column>
              </el-table>
            </div>
          </div>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { ElMessage } from 'element-plus';
import { Document, User, Menu, VideoPlay, ChatRound, Setting, SwitchButton } from '@element-plus/icons-vue';
import { authService } from '../api/authService';

const router = useRouter();
const userInfo = ref({ username: '', nickname: '', role: '' });

const activeMenu = computed(() => {
  return window.location.pathname;
});

const recentActivities = [
  { time: '2024-01-15 10:30', description: '新增视频类别：科技', user: 'admin' },
  { time: '2024-01-15 09:15', description: '审核通过视频：React入门教程', user: 'admin' },
  { time: '2024-01-14 16:45', description: '禁用用户账号：testuser', user: 'admin' },
  { time: '2024-01-14 14:20', description: '删除违规评论：123', user: 'admin' },
  { time: '2024-01-14 11:05', description: '更新系统设置', user: 'admin' }
];

const initData = async () => {
  try {
    const userStr = localStorage.getItem('user');
    if (userStr) {
      userInfo.value = JSON.parse(userStr);
    } else {
      const response = await authService.getProfile();
      if (response.code === 200) {
        userInfo.value = response.data;
        localStorage.setItem('user', JSON.stringify(response.data));
      }
    }
  } catch (error) {
    console.error('获取用户信息失败', error);
  }
};

const handleLogout = () => {
  localStorage.removeItem('token');
  localStorage.removeItem('user');
  ElMessage.success('退出登录成功');
  router.push('/login');
};

onMounted(() => {
  initData();
});
</script>

<style scoped>
.admin-layout {
  height: 100vh;
  background-color: #1a1a1a;
}

.sidebar {
  background-color: #2a2a2a;
  height: 100vh;
}

.logo {
  padding: 20px;
  text-align: center;
  border-bottom: 1px solid #3a3a3a;
}

.logo h3 {
  color: #ffffff;
  margin: 0;
}

.el-menu-vertical-dark {
  background-color: transparent;
  border-right: none;
}

.el-menu-item {
  color: #cccccc;
  margin: 0 10px;
  border-radius: 4px;
}

.el-menu-item:hover {
  background-color: #3a3a3a;
  color: #ffffff;
}

.el-menu-item.is-active {
  background-color: #409eff;
  color: #ffffff;
}

.header {
  background-color: #2a2a2a;
  color: #ffffff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  border-bottom: 1px solid #3a3a3a;
}

.user-info {
  margin-right: 20px;
  color: #cccccc;
}

.main-content {
  background-color: #1a1a1a;
  padding: 20px;
  overflow: auto;
}

.dashboard-content h2 {
  color: #ffffff;
  margin-bottom: 20px;
}

.stats-cards {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background-color: #2a2a2a;
  border: none;
  border-radius: 8px;
}

.stat-content {
  text-align: center;
  padding: 20px 0;
}

.stat-number {
  font-size: 32px;
  font-weight: bold;
  color: #409eff;
  margin-bottom: 10px;
}

.stat-label {
  font-size: 14px;
  color: #cccccc;
}

.recent-activities {
  background-color: #2a2a2a;
  padding: 20px;
  border-radius: 8px;
}

.recent-activities h3 {
  color: #ffffff;
  margin-bottom: 20px;
}

.el-table {
  background-color: transparent;
}

.el-table th {
  background-color: #3a3a3a;
  color: #ffffff;
  border-bottom: 1px solid #4a4a4a;
}

.el-table tr {
  background-color: #2a2a2a;
}

.el-table td {
  color: #cccccc;
  border-bottom: 1px solid #3a3a3a;
}

.el-table tr:hover > td {
  background-color: #3a3a3a;
}
</style>